﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Xml;
using QT.IO;

namespace myScheduler
{
    public partial class Timer : QT.Windows.Shell.GlassForm
    {
        #region Form

        public Timer()
        {
            InitializeComponent();
        }

        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            loadData();
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            base.OnClosing(e);
            e.Cancel = true;
            if (!File.Exists(Scheduler.myDirectory + @"\" + Scheduler.myDataFile)) Application.Exit();
            e.Cancel = false;
        }

        #endregion

        #region Event

        #region Button

        private void btnSave_Click(object sender, EventArgs e)
        {
            #region Verify Input

            if (string.IsNullOrEmpty(this.tbTitle.Text))
            {
                MessageBox.Show("请输入标题");
                return;
            }
            if (this.dtpDateEnd.Value.Date < this.dtpDateStart.Value.Date)
            {
                MessageBox.Show("开始日期必须小于或等于结束日期");
                return;
            }
            CheckTime();
            if ((int)this.dtpTimeStart.Value.TimeOfDay.TotalSeconds > (int)this.dtpTimeEnd.Value.TimeOfDay.TotalSeconds)
            {
                MessageBox.Show("开始时间必须小于或等于结束时间");
                return;
            }
            Regex regex = new Regex(@"^\d+$");
            if (this.cbbRepeatType.SelectedIndex != 0 && !regex.IsMatch(this.tbNumber.Text))
            {
                MessageBox.Show("请输入正确的重复数量");
                return;
            }
            if (this.rbtnSong.Checked && string.IsNullOrEmpty(this.tbPath.Text))
            {
                MessageBox.Show("请选择音乐文件路径");
                return;
            }

            #endregion

            ReleaseFile();

            #region Save

            XML myXml = null;

            try
            {
                myXml = new XML(Scheduler.myDirectory + @"\" + Scheduler.myDataFile);
                string guid = Guid.NewGuid().ToString().Replace("-", "");
                myXml.Insert("/root/list", "task", "guid", guid);
                myXml.Insert("/root/list/task[@guid='" + guid + "']", "title", "", this.tbTitle.Text);
                myXml.Insert("/root/list/task[@guid='" + guid + "']", "date", "", "");
                myXml.Insert("/root/list/task[@guid='" + guid + "']/date", "", "start", this.dtpDateStart.Value.ToString("yyyy-MM-dd"));
                myXml.Insert("/root/list/task[@guid='" + guid + "']/date", "", "end", this.dtpDateEnd.Value.ToString("yyyy-MM-dd"));
                myXml.Insert("/root/list/task[@guid='" + guid + "']", "time", "", "");
                myXml.Insert("/root/list/task[@guid='" + guid + "']/time", "", "start", this.dtpTimeStart.Value.ToString("HH:mm:ss"));
                myXml.Insert("/root/list/task[@guid='" + guid + "']/time", "", "end", this.dtpTimeEnd.Value.ToString("HH:mm:ss"));
                myXml.Insert("/root/list/task[@guid='" + guid + "']", "repeat", "type", this.cbbRepeatType.SelectedValue.ToString());
                myXml.Insert("/root/list/task[@guid='" + guid + "']/repeat", "", "", this.tbNumber.Text);
                myXml.Insert("/root/list/task[@guid='" + guid + "']", "alert", "type", this.rbtnMsgBox.Checked ? "0" : "1");
                if (!this.rbtnMsgBox.Checked) myXml.Insert("/root/list/task[@guid='" + guid + "']/alert", "", "", this.rbtnMsgBox.Checked ? "" : this.tbPath.Text);
                myXml.Insert("/root/list/task[@guid='" + guid + "']", "content", "", this.tbContent.Text);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }

            #endregion

            MessageBox.Show("操作成功");
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            OpenFileDialog myOpenFileDialog = new OpenFileDialog();
            myOpenFileDialog.ValidateNames = true;
            myOpenFileDialog.Multiselect = false;
            myOpenFileDialog.Filter = "所有支持的格式|*.mp3;*.wma|MEPG layer 3(mp3)|*.mp3|Windows Media Audio(wma)|*.wma";
            myOpenFileDialog.FilterIndex = 1;
            myOpenFileDialog.FileName = "";
            myOpenFileDialog.AddExtension = true;
            if (myOpenFileDialog.ShowDialog() == DialogResult.Cancel)
            {
                return;
            }
            this.tbPath.Text = myOpenFileDialog.FileName;
        }

        #endregion

        #region ComboBox

        private void cbbRepeatType_SelectedIndexChanged(object sender, EventArgs e)
        {
            CheckTime();

            if (this.cbbRepeatType.SelectedIndex == 0) this.tbNumber.Enabled = false;
            else this.tbNumber.Enabled = true;
        }

        #endregion

        #region RadioButton

        private void rbtnMsgBox_CheckedChanged(object sender, EventArgs e)
        {
            this.tbPath.Enabled = this.rbtnSong.Checked;
            this.btnOpen.Enabled = this.rbtnSong.Checked;
        }

        #endregion

        #endregion

        #region Method

        private void loadData()
        {
            ArrayList myList = new ArrayList();
            myList.Add(new DictionaryEntry("0", "不重复"));
            myList.Add(new DictionaryEntry("1", "分钟"));
            myList.Add(new DictionaryEntry("2", "小时"));
            myList.Add(new DictionaryEntry("3", "天"));
            myList.Add(new DictionaryEntry("4", "周"));
            myList.Add(new DictionaryEntry("5", "月"));
            myList.Add(new DictionaryEntry("6", "年"));
            this.cbbRepeatType.DisplayMember = "Value";
            this.cbbRepeatType.ValueMember = "Key";
            this.cbbRepeatType.DataSource = myList;
            this.cbbRepeatType.SelectedIndex = 0;
        }

        private void CheckTime()
        {
            List<string> array = new List<string>(new string[] { "0", "1", "2" });
            if (array.Contains(this.cbbRepeatType.SelectedValue.ToString()) && this.dtpTimeStart.Value.ToLongTimeString() != this.dtpTimeEnd.Value.ToLongTimeString())
            {
                MessageBox.Show("重复类型为["+this.cbbRepeatType.GetItemText(this.cbbRepeatType.SelectedItem)+"]时开始时间和结束时间必须一致");                
            }
        }

        private void ReleaseFile()
        {
            string dateXml = Scheduler.myDirectory + @"\" + Scheduler.myDataFile;
            if(!Directory.Exists(Scheduler.myDirectory)) Directory.CreateDirectory(Scheduler.myDirectory);
            if (!File.Exists(dateXml))
            {
                byte[] buffer = System.Text.Encoding.Default.GetBytes(Properties.Resources.Data);
                FileStream FS = new FileStream(dateXml, FileMode.Create);
                BinaryWriter BWriter = new BinaryWriter(FS);
                BWriter.Write(buffer, 0, buffer.Length);
                BWriter.Close();
                FS.Close();
            }
        }

        #endregion
    }
}
